﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using RssWindowsPhone.ServiceReference;

namespace RssWindowsPhone
{
    public partial class LoginPage : PhoneApplicationPage
    {



        // Constructor
        public LoginPage()
        {
            InitializeComponent();
        }

        private void connectBtn_Click(object sender, RoutedEventArgs e)
        {
            ServiceRssClient client = new ServiceRssClient(); 
            LoginModel log = new LoginModel()
            {
            Password = this.passwordBox.Password,
            UserName = this.loginBox.Text,
            RememberMe = false
            };

            try
            {
                client.LogOnAsync(log);
                client.LogOnCompleted += new EventHandler<LogOnCompletedEventArgs>(client_LogOnCompleted);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        void client_LogOnCompleted(object sender, LogOnCompletedEventArgs e)
        {
            MembershipCreateStatus connected = e.Result;
            
            string username = this.loginBox.Text;
            string password = this.passwordBox.Password;
            if (connected == MembershipCreateStatus.Success)
            {
                NavigationService.Navigate(new Uri("/CoreApp.xaml?User="+username+"&Password="+password, UriKind.Relative));
            }
            else
            {
                this.loginErrorMsg.Text = "";
                this.loginErrorMsg.Text = connected.ToString();
            }
        }

        private void registerBtn_Click(object sender, RoutedEventArgs e)
        {
            NavigationService.Navigate(new Uri("/Register.xaml", UriKind.Relative));
        }
    }
}